package models;

import helpers.GeneralHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import debugs.AuthorQuestionDebug;

import entities.AuthorQuestion;



public class AuthorQuestionModel {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "author_question_";
	
	private static AuthorQuestionModel instance = null;
	
	public static AuthorQuestionModel getInstance() {
		if (instance == null) {
			instance = new AuthorQuestionModel();
		}
		
		return instance;
	}
	
	private AuthorQuestionModel() {
		try {
			ObjectifyService.register(AuthorQuestion.class);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		cache = GeneralHelper.getCache();
		ofy = GeneralHelper.getObjectify();
	}
	
	public List<AuthorQuestion> list(int page, int limit) {
		List<AuthorQuestion> authorQuestions = new ArrayList<AuthorQuestion>();
		
		
		int start = (page - 1) * limit;
		authorQuestions = ofy.query(AuthorQuestion.class).limit(limit).offset(start).list();
		
		return authorQuestions;
	}
	
	public void insert(AuthorQuestion authorQuestion) {
		ofy.put(authorQuestion);
	}
	
	public void delete(AuthorQuestion authorQuestion) {
		ofy.delete(authorQuestion);
	}
	
	public AuthorQuestion getAuthorQuestion(String id) {
		AuthorQuestion authorQuestion = new AuthorQuestion();
		try {
			authorQuestion = ofy.get(new Key<AuthorQuestion>(AuthorQuestion.class, id));
		} catch (Exception e) {
			authorQuestion = null;
		}
		return authorQuestion;
	}
	
	public Map<String, AuthorQuestion> getByListIds(List<String> ids) {
		Map<String, AuthorQuestion> authorQuestions = new TreeMap<String, AuthorQuestion>();
		
		authorQuestions = ofy.get(AuthorQuestion.class, ids);
		
		return authorQuestions;
	}
	
	public void insertQuestion(String authorId, String qId) {
		AuthorQuestion authorQuestion = getAuthorQuestion(authorId);
		boolean isUpdate = false;
		
		if (authorQuestion == null) {
			authorQuestion = new AuthorQuestion();
			authorQuestion.setId(authorId);
		}

		List<String> qIds = authorQuestion.getqIds();
		if (!qIds.contains(qId)) {
			qIds.add(qId);
			isUpdate = true;
		}
		
		if (isUpdate) {
			authorQuestion.setqIds(qIds);
			insert(authorQuestion);
		}
		
	}
	
	public void insertAnswer(String authorId, String aId) {
		AuthorQuestion authorQuestion = getAuthorQuestion(authorId);
		boolean isUpdate = false;
		
		if (authorQuestion == null) {
			authorQuestion = new AuthorQuestion();
			authorQuestion.setId(authorId);
		}
		
		List<String> aIds = authorQuestion.getaIds();
		if (!aIds.contains(aId)) {
			aIds.add(aId);
			isUpdate = true;
		}
		
		if (isUpdate) {
			authorQuestion.setaIds(aIds);
			insert(authorQuestion);
		}
	}
	
	public boolean checkAuthorExists(String id) {
		boolean exists = false;
		AuthorQuestion authorQuestion = getAuthorQuestion(id);
		if (authorQuestion != null) {
			exists = true;
		}
		return exists;
	}
}
